#include<16f877a.h>
#include<stdio.h>
#include<string.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=11592000)
#use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7)
#define row1() init_lcd(0x80)	/* Begin at Line 1 */
#define row2() init_lcd(0xC0)   /* Begin at Line 2 */
#define LCD_Clear() init_lcd(0x01)	/* Clear display LCD */
unsigned char w[] = { 0x38,0x0c,0x01,0x06,0x80};
//unsigned int bp=0;
int j,flag=0;
unsigned char i=0,card_id[11],value;

int count=0;
/*======================================================LCD Initialisation============================================*/
void init_lcd(unsigned char j)
{
   output_e (0x00);
   output_d (j);
   output_e (0x02);
   delay_ms (2);
   output_e (0x00);
   delay_ms (2);
}
/*=======================================================Display Function=============================================*/
void display(char s)
{
   output_e (0x01);
   output_d (s);
   output_e (0x03);
   delay_ms (2);
   output_e (0x01);
   delay_ms (2);
}
/*=======================================================Functions For ROW============================================*/
void Lcd_init()
{
unsigned char i=0;
   for( i = 0 ; i < 6 ; i++ )
   {
      init_lcd (w[i]);
   }
}
void transmit(char a)
{
      putchar(a);
   delay_ms(100);

}
#BYTE RCREG = 0x1A           //Receive char
#INT_RDA
void SerialDataReceive()
{
	card_id[i]=RCREG;// Store the received data byte by byte
   value=RCREG;
	i++;
}
void main()
{
Lcd_init();
   enable_interrupts(INT_RDA);
   enable_interrupts(GLOBAL);
   port_b_pullups(true);
while(1)
{
   row1();
   display("Security System");
   delay_ms(100);
   row2();
   delay_ms(10);
   i=0;
   while(i<=6);
   
   if(card_id[6]=='8')
   {
      display("Student A");
      delay_ms(100);
   output_high(PIN_A3);
   delay_ms(500);
   i=0;
      transmit('a');
   }
   else if (card_id[6]=='4')
   {
      display("Student B");
      delay_ms(10);
   output_high(PIN_A3);
   delay_ms(500);
   i=0;
      transmit('b');
   }

   delay_ms(1000);
   i=0;
   while(i==0);

   delay_ms(100);
   row2();
   if(value=='k')
   {
      display("Open the Door");
      delay_ms(1000);
      delay_ms(1000);
      output_low(PIN_A3);
      delay_ms(1000);
   }
   else if(value=='w')
   {
      display("Warning");
      delay_ms(1000);
      delay_ms(1000);
      output_low(PIN_A3);
      delay_ms(1000);

   }
   i=0;
   flag=0;
   card_id[6]='0';
   LCD_Clear();
   }
}

